const express = require('express')
const router = express.Router()
const mw1 = (req,res,next)=> {
    console.log("这是局部中间件函数");
    next()
}

router.get('/',(req,res)=> {
    // res.setHeader('Content-Type','text/html;charset=utf-8')
    res.send('<h1>首页</h1>')
})
router.get('/user',mw1,(req,res)=> {
    // console.log(req.query);//获取请求参数
    const result = {
        msg: 'success',
        status: 700,
        data: { name:'陈俊宇', age: 27, gender: '男'},
        startTime: req.startTime
    }
    res.send(result)
})
router.get('/user/:userId', (req,res)=> {
    // console.log(req.params); //req.params 获取：后面的动态参数
    res.send(`您的用户ID是${req.params.userId}`)
})
router.post('/user',(req,res)=> {
    console.log('post请求参数：',req.body);
    res.send({
        msg: 'POST请求成功！',
        status: 700,
        data: [
            { name:'张三', age: 27, gender: '男' },
            { name:'李四', age: 23, gender: '男' },
            { name:'赵谦', age: 29, gender: '男' },
        ]
    })
})
router.get('/get',(req,res)=> {
    const query = req.query;
    res.send({
        status: 0,
        msg: 'GET请求成功!',
        data: query
    })
})
router.post('/post',(req,res)=>{
    const body = req.body;
    res.send({
        status: 0,
        msg: 'POST请求成功!',
        data: body
	})
})

module.exports = router